package com.ycz.shiroboot.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Description Shiro的配置类
 * @ClassName ShiroConfig
 * @Author yanchengzhi
 * @date 2021.09.25 15:35
 */
@Configuration
public class ShiroConfig {

    /*
     * @description: 注入UserRealm的bean
     * @param: []
     * @return: com.ycz.shiroboot.config.UserRealm
     * @author: yanchengzhi
     * @date: 2021/9/25 15:42
     */
    @Bean
    public UserRealm userRealm() {
        return new UserRealm();
    }

    /*
     * @description: 注入DefaultWebSecurityManager的bean，需要依赖定义的UserRealm对象
     * @param: [userRealm]
     * @return: org.apache.shiro.web.mgt.DefaultWebSecurityManager
     * @author: yanchengzhi
     * @date: 2021/9/25 15:47
     */
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 关联realm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    /*
     * @description: 注入ShiroFilterFactoryBean的bean,需要依赖DefaultWebSecurityManager对象
     * @param: [defaultWebSecurityManager]
     * @return: org.apache.shiro.spring.web.ShiroFilterFactoryBean
     * @author: yanchengzhi
     * @date: 2021/9/25 15:52
     */
//    @Bean
//    public ShiroFilterFactoryBean getShiroFilterFactoryBean(
//            @Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
//        // 设置安全管理器
//        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
//        return shiroFilterFactoryBean;
//    }
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(
            @Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
        // 设置安全管理器
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        // 添加Shiro常用的过滤器
        /**
         *  常用过滤器如下：
         *  anon：无需认证即可访问
         *  authc：必须经过认证才能访问
         *  user：使用记住我后可访问
         *  perms：拥有某个资源的权限才可访问
         *  role：拥有某个角色才可访问
         *
         */
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
//        filterChainDefinitionMap.put("/user/add","authc");
//        filterChainDefinitionMap.put("/user/edit","authc");
        //权限拦截，没有该权限会跳转到未授权页面
//        filterChainDefinitionMap.put("/user/add", "pers[user:add]");
//        filterChainDefinitionMap.put("/user/edit", "pers[user:update]");
        // 支持通配符配置
        //认证拦截
        filterChainDefinitionMap.put("/user/*", "authc");
        filterChainDefinitionMap.put("/shiro/logout", "logout");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        // 设置登录页面，没有认证或权限的会被定位到此路径
        shiroFilterFactoryBean.setLoginUrl("/toLogin");
        //设置未授权重定向路径，没有该权限的会被定位到此路径
//        shiroFilterFactoryBean.setUnauthorizedUrl("/noAuth");
        return shiroFilterFactoryBean;
    }


}
